/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Karyawan;

import Koneksi.KoneksiDatabase;
import UpdateKaryawan.updateKaryawanKontrol;
import hitunggaji.Gaji;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author yolan
 */
public class karyawankontrol {
    private Connection conn;

    public karyawankontrol(Connection conn2) {
        this.conn = conn2;
    }

    public static karyawankontrol getKoneksi() throws SQLException {
        KoneksiDatabase konDB = new KoneksiDatabase();
        karyawankontrol katkontrol;
        katkontrol = new karyawankontrol(KoneksiDatabase.getConn());
        return katkontrol;
    }
    public List<Karyawan> getAllKategori() throws SQLException {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        List<Karyawan> kategori = new ArrayList<Karyawan>();
        try {
            conn.setAutoCommit(false);
            String sql = "select * from karyawan order by nip";
            preparedStatement = conn.prepareStatement(sql);
            resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Karyawan kat = new Karyawan();
                kat.setNIP(resultSet.getString(1));
                kat.setNama(resultSet.getString(2));
                kat.setGolongan(resultSet.getString(3));
                kat.setStatus(resultSet.getString(4));
                kategori.add(kat);//tambah kategori baru
            }
            conn.commit();
        } catch (SQLException ex) {
            Logger.getLogger(updateKaryawanKontrol.class.getName()).log(Level.SEVERE, null, ex);
        }
        return kategori;
    }
    public Karyawan cariBerdasarkannip(String nip) throws SQLException {
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn.setAutoCommit(false);
            ps=conn.prepareStatement("select * from karyawan where nip=?");
            ps.setString(1, nip);
            rs=ps.executeQuery();

            Karyawan barang = null;
            if (rs.next()) {
                barang = new Karyawan();
                Karyawan kat = new Karyawan();
                barang.setNIP(rs.getString("nip"));
                barang.setNama(rs.getString("nama"));
                barang.setGolongan(rs.getString("golongan"));
//                barang.setStatus(rs.getString("status"));
                barang.setJumlahhadir(rs.getString("daftarhadir"));
                String nip2 = rs.getString(2);
//                double hadir=rs.getDouble(1);
                kat.setNIP(rs.getString(3));
                kat.setNama(nip2);
                kat.setGolongan(nip2);
                kat.setJumlahhadir(nip2);
                
            }
            conn.commit();
            return barang;
        } catch (SQLException ex) {
            conn.rollback();
            throw ex;
        }

    }

}
